#include "SerialPortDialog.h"

SerialPortDialog::SerialPortDialog(QWidget *parent, Qt::WindowFlags f)
    : QDialog(parent, f)
{
    QStringList strList;  
    isReceive = true;

    //initial serial
    port = new QextSerialPort(); 
    connect(port, SIGNAL(readyRead()), this, SLOT(updateReceiveWindow())); 
    port->setFlowControl(FLOW_OFF);

    rxData = new char[1024];

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(updateReceiveWindow()));
    timer->start(1);

    //initial dialog
    ui.setupUi(this);

    strList.clear();
    strList << "COM1" << "COM2" << "COM3" << "COM4" << "COM5" 
        << "COM6" << "COM7" << "COM8" << "COM9" << "COM10"
        << "COM11" << "COM12" << "COM13" << "COM14" << "COM15";
    ui.cmbPort->clear();
    ui.cmbPort->addItems(strList);
    ui.cmbPort->setCurrentIndex(5);

    strList.clear();
    strList << "300" << "600" << "1200" << "2400" << "4800" << "9600"
        << "19200" << "38400" << "57600" << "115200";
    ui.cmbBaudRate->clear();
    ui.cmbBaudRate->addItems(strList);
    ui.cmbBaudRate->setCurrentIndex(7);
    
    strList.clear();
    strList << "None" << "Odd" << "Even" << "Mark" << "Space";
    ui.cmbParity->clear();
    ui.cmbParity->addItems(strList);
    ui.cmbParity->setCurrentIndex(0);
    
    strList.clear();
    strList << "8" << "7" << "6";
    ui.cmbDatabits->clear();           
    ui.cmbDatabits->addItems(strList);
    ui.cmbDatabits->setCurrentIndex(0);
    
    strList.clear();
    strList << "1" << "1.5" << "2";
    ui.cmbStopbits->clear();
    ui.cmbStopbits->addItems(strList);
    ui.cmbStopbits->setCurrentIndex(0);

    strList.clear();
    strList << "Hex" << "Char";
    ui.cmbFormat->clear();
    ui.cmbFormat->addItems(strList);
    ui.cmbFormat->setCurrentIndex(0);
}

SerialPortDialog::~SerialPortDialog()
{
    port->close();    
    if (port != NULL)
        delete port;
    if (rxData != NULL)
        delete []rxData;
}

void SerialPortDialog::on_btnExit_clicked()
{
    this->close();
}

void SerialPortDialog::on_btnClear_clicked()
{
    ui.txtInScreen->clear();
    ui.ptxtOutScreen->clear();
}

void SerialPortDialog::on_btnSwitch_clicked()
{
    QString str;

    if (port->isOpen())
    {
        port->close();
        ui.btnSwitch->setText(tr("Open"));
    }
    else
    {
        //portName
        str = ui.cmbPort->currentText();
        portInfo = str;
        portInfo += "\r\n";
        port->setPortName(str);

        //baudRate
        str = ui.cmbBaudRate->currentText();
        portInfo += str;
        portInfo += "\r\n";
        switch(str.toInt())
        {
            case 300:
                port->setBaudRate(BAUD300);
                break;
            case 600:
                port->setBaudRate(BAUD600);
                break;
            case 1200:
                port->setBaudRate(BAUD1200);
                break;
            case 2400:
                port->setBaudRate(BAUD2400);
                break;
            case 4800:
                port->setBaudRate(BAUD4800);
                break;
            case 9600:
                port->setBaudRate(BAUD9600);
                break;
            case 19200:
                port->setBaudRate(BAUD19200);
                break;
            case 38400:
                port->setBaudRate(BAUD38400);
                break;
            case 57600:
                port->setBaudRate(BAUD57600);
                break;
            case 115200:
                port->setBaudRate(BAUD115200);
                break;
        }

        //dataBits
        str = ui.cmbDatabits->currentText();
        portInfo += str;
        portInfo += "\r\n";
        if (str == "8")
            port->setDataBits(DATA_8);
        else if (str == "7")
            port->setDataBits(DATA_7);
        else if (str == "6")
            port->setDataBits(DATA_6);

        //parity
        str = ui.cmbParity->currentText();
        portInfo += str;
        portInfo += "\r\n";
        if (str == "None")
            port->setParity(PAR_NONE);
        else if (str == "Odd")
            port->setParity(PAR_ODD);
        else if (str == "Mark")
            port->setParity(PAR_MARK);
        else if (str == "Even")
            port->setParity(PAR_EVEN);
        else if (str == "Space")
            port->setParity(PAR_SPACE);

        //stopBits
        str = ui.cmbStopbits->currentText();
        portInfo += str;
        portInfo += "\r\n";
        if (str == "1")
            port->setStopBits(STOP_1);
        else if (str == "1.5")
            port->setStopBits(STOP_1_5);
        else if (str == "2")
            port->setStopBits(STOP_2);

        port->open(QIODevice::ReadWrite);
        if (port->isOpen())
        {
            ui.btnSwitch->setText(tr("Close"));
            ui.ptxtOutScreen->setPlainText(portInfo);
        }
        else
        {
            ui.btnSwitch->setText(tr("Open"));
            portInfo += tr("Open False");
            ui.ptxtOutScreen->setPlainText(portInfo);
        }
    }
}

void SerialPortDialog::on_btnReceive_clicked()
{
    if (isReceive == true)
    {
        isReceive = false;
        ui.btnReceive->setText(tr("Receive"));
    }
    else
    {
        isReceive = true;
        ui.btnReceive->setText(tr("Pause"));
    }
}

void SerialPortDialog::on_btnSend_clicked()
{

}

void SerialPortDialog::updateReceiveWindow()
{
    qint64 len = port->bytesAvailable();
    if (len > 1024)
        len = 1024;

    if (len > 0)
    {
        ui.txtInScreen->append(QString::number(len,10));
        port->read(rxData, len);
        if (isReceive == true)
        {
            ui.ptxtOutScreen->moveCursor(QTextCursor::End);
            ui.ptxtOutScreen->insertPlainText(QString::fromAscii(rxData, len));
        }
    }
}
